﻿using System;
using System.Net;
using System.Reflection;
using HttpServer.Logging;
using HttpServer.Mvc;
using HttpServer.Resources;
using HttpServer.Routing;
using HttpServer.ViewEngine.Spark;
using HttpListener=HttpServer.HttpListener;
using HttpServer.Modules;
using HttpServer.BodyDecoders;
using HttpServer;
using System.Configuration;

namespace BlackBBSWebServer
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // Log everything to console.
            LogFactory.Assign(new ConsoleLogFactory(null));

            Assembly thisAssembly = typeof (Program).Assembly;

            // create a MVC web server.
            var server = new MvcServer();
            server.ViewEngines.Add(new SparkEngine());
            server.Add(HttpListener.Create(IPAddress.Any, 80));
            server.Add(new SimpleRouter("/", "/home/"));

            var module = new FileModule();
            var filesPath = ConfigurationManager.AppSettings["filesPath"];
            module.Resources.Add(new FileResources("/", filesPath));
            server.Add(module);
            server.InvokingAction += new EventHandler<ControllerEventArgs>(server_InvokingAction);
            server.Add(new MultiPartDecoder());

            // Load controllers and embedded views.
            BootStrapper bootStrapper = new BootStrapper(server);
            bootStrapper.LoadEmbeddedViews(thisAssembly);
            bootStrapper.LoadControllers(thisAssembly);

            // And run the server.
            server.Start(5);

            // run until you press enter.
            Console.ReadLine();
        }

        /// <summary>
        /// This is fired before an action request is processed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void server_InvokingAction(object sender, ControllerEventArgs e)
        {            
        }

        //private static void OnRequest(object sender, RequestEventArgs e)
        //{
        //    if (e.Request.Method == Method.Post)
        //    {

        //    }
        //}

    }
}